package com.intel.factorytest.activity;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.intel.factorytest.R;
import com.intel.factorytest.application.MATApplication;
import com.intel.factorytest.cfparser.ConfigFileParseUtil;
import com.intel.factorytest.cfparser.concrete.AConfigFileParser;
import com.intel.factorytest.cfparser.concrete.PropertiesConfParser;
import com.intel.factorytest.constants.PropertiesConstants;
import com.intel.factorytest.log.LogWriter;

public class ServiceTestsMenuActivity extends ListActivity {

    private static final String TAG = "ServiceTestsMenuActivity";
    private static final String DEFAULT_SDCARD_PATH = "/storage/emulated/0"; // internal storage card
    //    SimpleAdapter adapter = null;
    List<Map<String, Object>> testMenuItems = null;
    private File sampleDir;
    private File resultDir;
    private Boolean mSkip = true;
    private static final int START_TEST = 5;     
    private int test_pos = -1;     
    private static final String TEST_ITEM = "test_item";
    private static final String ITEM_NAME = "item_name";
    private String tName;
    ArrayList<Integer> testFail = new ArrayList<Integer>();
    private int testItemNum = -1;     
    static final String TEST_SAVE_RESULT = "oms.action.intent.SAVE_RESULT";
    static final String TEST_SAVE_RESULT_SERVICE = "oms.action.intent.SAVE_RESULT_SERVICE";
    private TestListAdapter mAdapter;
    private List<Integer> mData = new ArrayList<Integer>();
    private boolean isFinish = false;
    private String sys_service ="SYSTEM_SERVICE_TEST";
    private String sys_service_for_pcba ="SYSTEM_SERVICE_FOR_PCBA_TEST";
    private String sdcard_config ="SDCARD_CONFIG";
    private int is_sys = 0;
    private int is_sys_for_pcba = 0;
    private static final String TEST_ITEM_RESULT = "test_item_result";
    private boolean is_pass;
    private String sdFilePath = "";
    private String sdOrientation = "";
    
    // log
    private String logFileName;
    
    // Locale
//    private Locale locale;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {        
        super.onCreate(savedInstanceState);Log.e(TAG,"onCreate");
        System.out.println("tangjun ServiceTestsMenuActivity :: onCreate");
        IntentFilter filter = new IntentFilter();
        filter.addAction(TEST_SAVE_RESULT);
        registerReceiver(mIntentReceiver, filter, "permission.CIT_ALLOW_BROADCAST", null);
        Intent mIntent = getIntent();
        is_sys = mIntent.getIntExtra(sys_service, 0);
        is_sys_for_pcba = mIntent.getIntExtra(sys_service_for_pcba, 0);
        sdFilePath = mIntent.getStringExtra(sdcard_config);
        logFileName = mIntent.getStringExtra(LogWriter.LogFileNameTag);
        
        updateConfiguration();
        
        if(1 == is_sys) {
            if(1 == is_sys_for_pcba) {
                setTitle(getString(R.string.SERVICEMENU_SYSTEM_SERVICETESTS_FOR_PCBA_TXT));
            } else {
                setTitle(getString(R.string.SERVICEMENU_SYSTEM_SERVICETESTS_TXT));
            }
        }
                Configuration conf = getResources().getConfiguration();
        String localeString = conf.locale.getCountry();
        Log.i(TAG,"is_sys =" + is_sys + "is_sys_for_pcba =" + is_sys_for_pcba + "  localeString = " + localeString );
//        testMenuItems = new ServiceTestsItemMenu(this).showTestsMenuMap(is_sys, is_sys_for_pcba, localeString, sdFilePath);
        testMenuItems = ConfigFileParseUtil.getConfParserConcrete(this, AConfigFileParser.EXTENSION_PROPERTISE).doParse(is_sys, is_sys_for_pcba, localeString, sdFilePath);
        
        // log
        Iterator<Map<String, Object>> it = testMenuItems.iterator();
        while(it.hasNext()) {
            Map<String, Object> map = it.next();
            String itemName = (String) map.get("itemName");
            Intent intent = (Intent) map.get("intent");
            String className = intent.getComponent().getClassName();
            Log.i(TAG, "tangjun itemName=" + itemName + ", className=" + className);
            
        }
        // log end
        
//        sdOrientation = Util.readSDConfigByLines(sdFilePath, is_sys_for_pcba);
        sdOrientation = ((PropertiesConfParser) ConfigFileParseUtil.getConfParserConcrete(this, AConfigFileParser.EXTENSION_PROPERTISE))
                .getProperties(is_sys_for_pcba == 1 ? true : false)
                .getProperty(PropertiesConstants.KEY_ORIENTATION);
        
        boolean isSystemWithConfigOrientation = false;
        if (is_sys == 1 && is_sys_for_pcba == 0 && sdOrientation != null) { // 在整机测试配置文件中配置了屏幕方向
            isSystemWithConfigOrientation = true;
        }
        
        if (is_sys_for_pcba == 1 || isSystemWithConfigOrientation) {
            int orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
            if(sdOrientation.equals("SCREEN_ORIENTATION_PORTRAIT")) {
                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                super.setRequestedOrientation(orientation);
                
            } else if(sdOrientation.equals("SCREEN_ORIENTATION_LANDSCAPE")) {
                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                super.setRequestedOrientation(orientation);
                
            }
        }


        mData = getImgSource();
        mAdapter = new TestListAdapter(this,testMenuItems,mData);
        /*
          adapter = new SimpleAdapter(this, testMenuItems, android.R.layout.simple_list_item_1,
          new String[]{"itemName"}, new int[]{android.R.id.text1});
        */
        setListAdapter(mAdapter);
        mSkip = hasSetStatus();
        testItemNum = testMenuItems.size();
        Log.e(TAG,"testItemNum ="+testItemNum);

    }
    
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Resources resourse = this.getResources();
        Configuration config = resourse.getConfiguration();
        SharedPreferences pre = getSharedPreferences(MATApplication.LANGUAGE_PREF, Context.MODE_PRIVATE);
        String lanPre = pre.getString(MATApplication.LANGUAGE_KEY, "zh");
        System.out.println("tangjun preference lan = " + lanPre);
        if (lanPre.equals("zh")) {
            config.locale = Locale.SIMPLIFIED_CHINESE;
        } else {
            config.locale = Locale.ENGLISH;
        }
        Locale.setDefault(config.locale);
        this.getBaseContext().getResources().updateConfiguration(config, resourse.getDisplayMetrics());
    }
    
    protected void updateConfiguration() {
        Resources resourse = this.getResources();
        Configuration config = resourse.getConfiguration();
        SharedPreferences pre = getSharedPreferences(MATApplication.LANGUAGE_PREF, Context.MODE_PRIVATE);
        String lanPre = pre.getString(MATApplication.LANGUAGE_KEY, "zh");
        System.out.println("tangjun preference lan = " + lanPre);
        if (lanPre.equals("zh")) {
            config.locale = Locale.SIMPLIFIED_CHINESE;
        } else {
            config.locale = Locale.ENGLISH;
        }
        Locale.setDefault(config.locale);
        this.getBaseContext().getResources().updateConfiguration(config, resourse.getDisplayMetrics());
    }
    
    protected void changeLanguage(Locale locale) {
        //应用内配置语言
        Resources resources =getResources();//获得res资源对象  
        Configuration config = resources.getConfiguration();//获得设置对象  
        DisplayMetrics dm = resources.getDisplayMetrics();//获得屏幕参数：主要是分辨率，像素等。
        config.locale = locale;
        resources.updateConfiguration(config, dm);
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Map map =  (Map) l.getItemAtPosition(position);
        Intent intent = (Intent) map.get("intent");
        intent.putExtra(TEST_ITEM, position);
        intent.putExtra("sd_orientation",sdOrientation);
        intent.putExtra(sys_service,is_sys);
        intent.putExtra(sys_service_for_pcba, is_sys_for_pcba);
        System.out.println("tangjun itemName=" + (String) map.get("itemName"));
        intent.putExtra(ITEM_NAME, (String) map.get("itemName")/*(String) PropertiesConfParser.TestCaseName.get((String) map.get("itemName"))*/);
        intent.putExtra(LogWriter.LogFileNameTag, logFileName);
//        intent.putExtra("Locale", locale);
        Log.e("util","sdFilePath ="+sdFilePath);
        tName = (String)map.get("itemName");    
        Log.e(TAG,"onListItemClick ="+tName);
        mSkip = true;
        if (mSkip && is_sys==0) {
            try{
                // startActivity(intent);
                StringBuilder builder = new StringBuilder();
                Set<String> keys = intent.getExtras().keySet();
                Iterator<String> it = keys.iterator();
                while (it.hasNext()) {
                    String key = it.next();
                    builder.append(intent.getExtras().get(key)).append("\n");
                }
                
                Log.i(TAG, "tangjun see step1" + intent.getComponent().getClassName() + "\nposition" + position);
                startActivityForResult(intent,START_TEST);
                
                
            }catch(Exception e){
                Log.v(TAG,"");
            }
        }else if(position == test_pos +1){
            try{
                // startActivity(intent);
                Log.i(TAG, "tangjun see step2");
                startActivityForResult(intent,START_TEST);
            }catch(Exception e){
                Log.v(TAG,"");
            }
        } else if(test_pos + 1 == l.getCount() && 0 == position) {
            try{
                Log.i(TAG, "tangjun see step3");
                startActivityForResult(intent,START_TEST);
            }catch(Exception e){ 
                 Log.v(TAG,"");
            }
        } else {
            Toast.makeText(ServiceTestsMenuActivity.this, getString(R.string.set_testmenu), Toast.LENGTH_SHORT).show();
            Log.d(TAG,"don't skip the item !!");
        }
    }
    private boolean hasSetStatus () {
        sampleDir = new File("/data/data/com.intel.factorytest/setmode.txt");
        if (sampleDir == null || !sampleDir.exists()) {
            return true;

        } else {
            return false;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.e(TAG,"requestCode = " + requestCode + " resultCode = " + resultCode + " data = " + data);
        System.out.println("tangjun OnMenuActivityResult: requestCode = " + requestCode + " resultCode = " + resultCode + " data = " + data + " tName=" + tName);
        if (data != null && tName != null) {
            is_pass = data.getBooleanExtra(TEST_ITEM_RESULT,false);
            test_pos = data.getIntExtra(TEST_ITEM, -1);
            boolean isLogSuc = data.getBooleanExtra(BaseActivity.TEST_LOG_RESULT, true);
//            System.out.println("tangjun is_sys = " + is_sys);
//            if (is_sys == 1 && !isLogSuc) {
//                try {
//                    LogWriter.write(data.getStringExtra(LogWriter.LogFileNameTag), LogWriter.TestCase.getSingletonInstanse(0, "Simcard", "", is_pass));
//                } catch (IOException e) {
//                    // TODO Auto-generated catch block
//                    e.printStackTrace();
//                }
//            }
            Log.e(TAG,"pos = " + test_pos + " name = " + tName);
            /*if (1 == is_sys) {
                ListView lv = this.getListView();
                if (test_pos + 1 == lv.getCount()) {
                    mData.set(test_pos,R.drawable.btn_check_on);
                    mAdapter.notifyDataSetChanged();
                }
                if (test_pos + 1 < lv.getCount()) {
                    mData.set(test_pos,R.drawable.btn_check_on);
                    mAdapter.notifyDataSetChanged();
                    Map map =  (Map)lv.getItemAtPosition(test_pos + 1);
                    Intent intent = (Intent) map.get("intent");
                    intent.putExtra(TEST_ITEM, test_pos + 1);
                    tName = (String)map.get("itemName");
                    startActivityForResult(intent,START_TEST);    
                }
                return;
            }*/
        
            
            if (tName.equals("Accelerometer") || tName.equals("加速计") || tName.equals("Compass")
                || tName.equals("指南针") || tName.equals("GyroTest") || tName.equals("陀螺仪")
                || tName.equals("WLAN") || tName.equals("WIFI")
                || tName.equals("Brightness") || tName.equals("亮度")
                || tName.equals("USB")
                || tName.equals("OTG(Mouse,Udisk)") || tName.equals("OTG(鼠标,U盘)")
                || tName.equals("按键测试") || tName.equals("Keyboard & switch")
                || tName.equals("Battery status check") || tName.equals("电池状态检验")
                || tName.equals("Internal/External memory") || tName.equals("内部/外部存储器")
                || tName.equals("Bluetooth") || tName.equals("蓝牙") 
                || tName.equals("Touch Panel") || tName.equals("触摸屏测试")
                || tName.equals("Touch Screen") || tName.equals("电子笔测试")
                || tName.equals("Display")  || tName.equals("显示")
                || tName.equals("Speaker & Headset") || tName.equals("扬声器和耳机测试")
                || tName.equals("Microphone") || tName.equals("麦克风")
                || tName.equals("MicrophoneSub") || tName.equals("副麦克风")
                || tName.equals("HeadsetMicrophone") || tName.equals("耳机麦克风")
                || tName.equals("WIFI & Bluetooth") || tName.equals("WIFI和蓝牙")
                || tName.equals("Video") || tName.equals("视频")
                || tName.equals("Simcard") || tName.equals("Sim卡")
                || tName.equals("Sensors") || tName.equals("传感器")
                || tName.equals("ComprehensiveTest") || tName.equals("综合测试")
                || tName.equals("HDMI")
                || tName.equals("Vibrator") || tName.equals("振动")
                || tName.equals("Speaker") || tName.equals("扬声器")//左右声道 // modify by wangsm
                || tName.equals("System Camera") || tName.equals("系统照相机")
                || tName.equals("GPSHot") || tName.equals("GPS")
                || tName.equals("Front Camera") || tName.equals("前置照相机")
                || tName.equals("Back Camera") || tName.equals("后置照相机")
                || tName.equals("Camera") || tName.equals("摄像头")
                || tName.equals("Software Information") || tName.equals("软件信息")
                || tName.equals("Call Test") || tName.equals("通话测试")
                || tName.equals("FM Test") || tName.equals("FM测试")
                || tName.equals("Receiver") || tName.equals("听筒测试")
                || tName.equals("Pogo")|| tName.equals("Pogo键盘测试")) {
                Log.e(TAG,"if");
                if (is_pass) {
                    mData.set(test_pos,R.drawable.btn_check_on);
                    mAdapter.notifyDataSetChanged();
                    if (testFail.contains(test_pos)) {
                        testFail.remove(testFail.indexOf(test_pos));
                    }
                    if (1 == is_sys) {
                        ListView lv = ServiceTestsMenuActivity.this.getListView();
                        if (test_pos + 1 == lv.getCount()) {
                        }
                        if (test_pos + 1 < lv.getCount()) {
                            Map map =  (Map)lv.getItemAtPosition(test_pos + 1);
                            Intent intent = (Intent) map.get("intent");
                            intent.putExtra(TEST_ITEM, test_pos + 1);
                            intent.putExtra("sd_orientation",sdOrientation);
                            intent.putExtra(sys_service,is_sys);
                            intent.putExtra(sys_service_for_pcba, is_sys_for_pcba);
                            System.out.println("tangjun itemName=" + (String) map.get("itemName"));
                            intent.putExtra(ITEM_NAME, (String) map.get("itemName")/*(String) PropertiesConfParser.TestCaseName.get((String) map.get("itemName"))*/);
                            intent.putExtra(LogWriter.LogFileNameTag, logFileName);
                            tName = (String)map.get("itemName");
                            ServiceTestsMenuActivity.this.startActivityForResult(intent,START_TEST);
                            return;    
                        }
                        
        
                    }
                } else {
                    if (testFail.contains(test_pos)) {
                        testFail.remove(testFail.indexOf(test_pos));
                    }
                    testFail.add(test_pos);
                    mData.set(test_pos,R.drawable.btn_close_normal);
                    mAdapter.notifyDataSetChanged();
                    if (1 == is_sys) {
                        ListView lv = ServiceTestsMenuActivity.this.getListView();
                        if (test_pos + 1 == lv.getCount()) {
                        }
                        if (test_pos + 1 < lv.getCount()) {
                            Map map =  (Map)lv.getItemAtPosition(test_pos + 1);
                            Intent intent = (Intent) map.get("intent");
                            intent.putExtra(TEST_ITEM, test_pos + 1);
                            intent.putExtra("sd_orientation",sdOrientation);
                            intent.putExtra(sys_service,is_sys);
                            intent.putExtra(sys_service_for_pcba, is_sys_for_pcba);
                            System.out.println("tangjun itemName=" + (String) map.get("itemName"));
                            intent.putExtra(ITEM_NAME, (String) map.get("itemName")/*(String) PropertiesConfParser.TestCaseName.get((String) map.get("itemName"))*/);
                            intent.putExtra(LogWriter.LogFileNameTag, logFileName);
                            tName = (String)map.get("itemName");
                            ServiceTestsMenuActivity.this.startActivityForResult(intent,START_TEST);
                            return;    
                        }
                        
        
                    }

                }
            } else {
                Log.e(TAG,"else");
                showDialog(0);

            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();Log.e(TAG,"onResume");
    }
    @Override
    protected void onPause() {
        super.onPause();Log.e(TAG,"onPause");
    }
    @Override
    protected Dialog onCreateDialog(int none){

        AlertDialog.Builder builder = new AlertDialog.Builder(ServiceTestsMenuActivity.this)
//            .setIcon(R.drawable.alert_dialog_icon)
            .setTitle(R.string.SERVICEMENU_SHOW_RESULT)
            .setMessage("")
            .setPositiveButton(R.string.SERVICEMENU_DIALOG_YES_TXT, new DialogInterface.OnClickListener() {
                

                    public void onClick(DialogInterface dialog, int which) {
                        if (1 == is_sys) {
                            ListView lv = ServiceTestsMenuActivity.this.getListView();
                            if (test_pos + 1 == lv.getCount()) {
                            }
                            if (test_pos + 1 < lv.getCount()) {
                                mData.set(test_pos,R.drawable.btn_check_on);
                                mAdapter.notifyDataSetChanged();
                                if (testFail.contains(test_pos)) {
                                    testFail.remove(testFail.indexOf(test_pos));
                                }
                                Map map =  (Map)lv.getItemAtPosition(test_pos + 1);
                                Intent intent = (Intent) map.get("intent");
                                intent.putExtra(TEST_ITEM, test_pos + 1);
                                intent.putExtra("sd_orientation",sdOrientation);
                                tName = (String)map.get("itemName");
                                ServiceTestsMenuActivity.this.startActivityForResult(intent,START_TEST);
                                return;    
                            }
                            
            
                        }
                        mData.set(test_pos,R.drawable.btn_check_on);
                        mAdapter.notifyDataSetChanged();
                        if (testFail.contains(test_pos)) {
                            testFail.remove(testFail.indexOf(test_pos));
                        }
                        return;
                    }
            })
            .setNegativeButton(R.string.SERVICEMENU_DIALOG_NO_TXT, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        if (1 == is_sys) {
                            ListView lv = ServiceTestsMenuActivity.this.getListView();
                            if (test_pos + 1 == lv.getCount()) {
                            }
                            if (test_pos + 1 < lv.getCount()) {
                                if (testFail.contains(test_pos)) {
                                    testFail.remove(testFail.indexOf(test_pos));
                                }
                                testFail.add(test_pos);
                                mData.set(test_pos,R.drawable.btn_close_normal);
                                mAdapter.notifyDataSetChanged();
                                Map map =  (Map)lv.getItemAtPosition(test_pos + 1);
                                Intent intent = (Intent) map.get("intent");
                                intent.putExtra(TEST_ITEM, test_pos + 1);
                                intent.putExtra("sd_orientation",sdOrientation);
                                tName = (String)map.get("itemName");
                                ServiceTestsMenuActivity.this.startActivityForResult(intent,START_TEST);
                                return;    
                            }
                            
            
                        }
                        if (testFail.contains(test_pos)) {
                            testFail.remove(testFail.indexOf(test_pos));
                        }
                        testFail.add(test_pos);
                        mData.set(test_pos,R.drawable.btn_close_normal);
                        mAdapter.notifyDataSetChanged();
                        return;
                    }
                    });
            AlertDialog dialog = builder.create();
            dialog.setCancelable(false);
            return dialog;
    }
    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {
        String msg = tName + " " + getString(R.string.SERVICEMENU_IS_SUCCESS);//" is Ok?";
        ((AlertDialog)dialog).setMessage(msg);
    }
    private void WriteResultFile() {
        BufferedWriter writer=null;
        try {
            writer = new BufferedWriter(new FileWriter("/data/data/com.intel.factorytest/result.txt"));

            Log.e(TAG,"testItemNum ="+testItemNum+"test_pos ="+test_pos+"size is "+testFail.size());
            if (testFail.size() == 0 && test_pos == testItemNum-2) {
                writer.write("pass");
                writer.newLine();
                writer.flush();
            }else {
                for (int i = 0 ;i< testFail.size();i++) {
                    writer.write(testFail.get(i).toString());
                    writer.newLine();
                    writer.flush();
                }    
            }    
        } catch (IOException e) {
            Log.e(TAG,"write result failed");
        }finally{
            try {
                writer.close();
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }
    }
    //this fun for testing 
    private void ReadResultFile() {
        BufferedReader reader=null;
        String s=null;
        try {
            reader = new BufferedReader(new FileReader("/data/data/com.intel.factorytest/result.txt"));
            while ((s = reader.readLine()) != null) {
                Log.e(TAG,"s =="+s);
            }        
        } catch (IOException e) {
            Log.e(TAG,"read  result failed");
        }finally{
            try {
                reader.close();
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }
    }
    @Override
    public void finish() {
        if (!isFinish) {
            WriteResultFile();
            unregisterReceiver(mIntentReceiver);
            Intent intent = new Intent(TEST_SAVE_RESULT_SERVICE);
            sendBroadcast(intent, "permission.CIT_ALLOW_INCOMING");
            isFinish = true;
        }
        super.finish();
    }
    private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();             
                if (action.equals(TEST_SAVE_RESULT)) {
                    WriteResultFile(); 
                } 
            }
    };

    private class TestListAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        private List<Map<String, Object>> summary;
        private List<Integer> imgSource;


        public void notifyDataSetChanged() {  
            super.notifyDataSetChanged();  
        }

        public TestListAdapter(Context context,
                List<Map<String, Object>> summarys,List<Integer> imgSources)
        {
            mInflater = LayoutInflater.from(context);
            summary = summarys;
            imgSource = imgSources;
        }

        public int getCount()
        {
            return summary.size();
        }

        public Object getItem(int position)
        {
            return summary.get(position);
        }

        public long getItemId(int position)
        {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup par)
        {
            ViewHolder holder; 
            String tName; 
            if(convertView == null)
            {
                convertView = mInflater.inflate(R.layout.test_list,null);
                holder = new ViewHolder();
                holder.text2=(TextView)convertView.findViewById(R.id.TextView2);
                holder.mImageView=(ImageView)convertView.findViewById(R.id.check);   
                convertView.setTag(holder);

            }
            else
            {
                holder = (ViewHolder) convertView.getTag();
            }


            Map map =  (Map) summary.get(position);
            tName = (String)map.get("itemName");    

            // holder.mImageView.setBackgroundResource(imgSource.get(position));//R.drawable.btn_check_off); 
            holder.text2.setText(tName);
            //holder.text2.setTextSize(24);
            holder.mImageView.setBackgroundResource(imgSource.get(position));//R.drawable.btn_check_off); 
            return convertView;
        }

        /* class ViewHolder */
        public class ViewHolder
        {
            ImageView mImageView;
            TextView text2;
        }
    }
    private List<Integer> getImgSource () {
        ArrayList<Integer> testList = new ArrayList<Integer>();
        for (int i =0;i<testMenuItems.size();i++) {
            testList.add(R.drawable.btn_check_off);
        }
        return testList;
    }
}
